import { createRouter, createWebHashHistory } from 'vue-router';
import { useUserStore } from '@/store';
import { addRoutes } from '@/util/anyncRoutes';
import { rootRoutes } from '../mock/routes';
import { routedate } from '../mock/JsonData'

const routes = [
    {
        path: '/',
        name: 'home',
        component: () => import('@/views/Home/index.vue'),
        meta: {
            title: '我的站点',
            layout: true,
            menu: true,
            icon: 'https://gitee.com/xiaomao12/images/raw/master/img/home.png',
            sort: 1,
        },
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

let registerRouteFresh = true;
router.beforeEach((to, from, next) => {
    if (registerRouteFresh) {
        const userStore = useUserStore();
        userStore.setRoutes(routedate);
        addRoutes(userStore, router);
        registerRouteFresh = false;
        router.push({ path: '/' });
    } else {
        next();
    }
});

export default router;
